﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using sil;
using Visifire.Charts;
using System.IO;
using System.Windows.Media.Imaging;

namespace GisWeb
{
    public partial class PredictWindow : FloatableWindow
    {

        public int showFlag = 0;
        public PredictWindow()
        {
            InitializeComponent();
        }

        private void combo_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (showFlag == 1) {
                int select = (sender as ComboBox).SelectedIndex;
                chart1 predict =LayoutRoot.Children[0] as chart1;
                switch (select) {
                    case 0: foreach (DataSeries i in predict.Series) { i.RenderAs = RenderAs.Column; } break;
                    case 1: foreach (DataSeries i in predict.Series) { i.RenderAs = RenderAs.Bar; } break;
                    case 2: foreach (DataSeries i in predict.Series) { i.RenderAs = RenderAs.StackedColumn; } break;
                    case 3: foreach (DataSeries i in predict.Series) { i.RenderAs = RenderAs.StackedArea; } break;
                
                
                }
            
            
            
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            saveImg(LayoutRoot);
        }

        //点击OK时将选中的值存储起来
     
    private void saveImg(UIElement grid) {
            SaveFileDialog dialog = new SaveFileDialog();
            dialog.Filter = "支持的图像文文件(*.png)|*.png";
            if (dialog.ShowDialog() != true)
            {
                return;
            }
            System.Windows.Media.Imaging.WriteableBitmap bitmap = new WriteableBitmap(grid, null);
            int[] i = bitmap.Pixels;
            EditableImage imageData = new EditableImage(bitmap.PixelWidth, bitmap.PixelHeight);


            for (int y = 0; y < bitmap.PixelHeight; ++y)
            {
                for (int x = 0; x < bitmap.PixelWidth; ++x)
                {

                    int pixel = i[bitmap.PixelWidth * y + x];

                    imageData.SetPixel(x, y,
                                (byte)((pixel >> 16) & 0xFF),
                                (byte)((pixel >> 8) & 0xFF),
                                (byte)(pixel & 0xFF),
                                (byte)((pixel >> 24) & 0xFF)
                                );
                }
            }
            Stream pngStream = imageData.GetStream();

            byte[] buffer = new byte[pngStream.Length];
            pngStream.Read(buffer, 0, (int)pngStream.Length);
            pngStream.Dispose();

            Stream st = dialog.OpenFile();
            st.Write(buffer, 0, buffer.Length);
            st.Close();
        }
    
    }
    }


